package com.example.mentalhealth.Service;

import com.example.mentalhealth.entities.ExamRecord;
import com.example.mentalhealth.entities.ExamRecordQuestion;
import com.example.mentalhealth.entities.ExamRecordResultData;
import com.example.mentalhealth.entities.Question;
import com.example.mentalhealth.mapper.ExamRecordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class ExamRecordService {

    @Autowired
    public ExamRecordMapper examRecordMapper;

    @Autowired
    public ExamPaperService examPaperService;

    @Autowired
    public ExamPaperQuestionService examPaperQuestionService;

    @Autowired
    public UserService userService;

    @Autowired
    public ExamRecordQuestionService examRecordQuestionService;

    @Autowired
    public QuestionService questionService;

    //查询答卷
    public PageInfo<ExamRecordResultData> selectByQuery(ExamRecord examRecord,
                                                        LocalDateTime startTime,
                                                        LocalDateTime endTime,
                                                        int startScore, int endScore,int pageNum, int pageSize){

        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        //执行查询
        List<ExamRecord> examRecordList = examRecordMapper.selectByQuery(examRecord,startTime,endTime,startScore,endScore);
        System.out.println(examRecordList);
        //将数据封装到ExamRecordResultData
        List<ExamRecordResultData>  examRecordResultData = new ArrayList<>();

        for (ExamRecord examRecord1 : examRecordList) {
            ExamRecordResultData examRecordResultData1 = new ExamRecordResultData();
            examRecordResultData1.setId(examRecord1.getId());//答卷记录id
            //通过试卷id获取试卷标题
            examRecordResultData1.setPaperTitle(examPaperService.getTitleById(examRecord1.getPaperId()));//答卷标题
            //通过用户id获取用户姓名
            examRecordResultData1.setStudentName(userService.queryUserById(examRecord1.getUserId()).getUsername());
            //设置分数
            examRecordResultData1.setScore(examRecord1.getScore());
            //设置答题时间
            examRecordResultData1.setUseTime(examRecord1.getDuration());
            //设置提交时间
            examRecordResultData1.setSubmitTime(examRecord1.getSubmitTime());
            //设置是否及格
            BigDecimal passScore = examPaperService.queryById(examRecord1.getPaperId()).getPassScore();//`
            if (examRecord1.getScore().compareTo(passScore) >= 0){
                examRecordResultData1.setPassStatus(true);//通过及格分数判断是否及格
            }else {
                examRecordResultData1.setPassStatus(false);
            }
            //查询问题列表
            List<ExamRecordQuestion> examRecordQuestions
                    =examRecordQuestionService.queryByExamRecordId(examRecord1.getId());;//问题列表
            List<ExamRecordQuestion> examRecordQuestions1 = new ArrayList<>();
            for (ExamRecordQuestion examRecordQuestion : examRecordQuestions){
                //通过题目id查询题目信息
                Question question = questionService.queryQuestionsByID(Math.toIntExact(examRecordQuestion.getQuestionId()));
                //设置
                examRecordQuestion.setType(question.getType());//设置题目类型
                examRecordQuestion.setContent(question.getContent());//设置题目内容
                examRecordQuestion.setOptions(question.getOptions());//设置选项
                examRecordQuestion.setCorrectAnswer(question.getAnswer());//设置正确答案
                examRecordQuestion.setExplanation(question.getExplanation());//设置解释
                //封装
                examRecordQuestions1.add(examRecordQuestion);
            }

            examRecordResultData1.setQuestions(examRecordQuestions1);


            examRecordResultData.add(examRecordResultData1);
        }

        return new PageInfo<>(examRecordResultData);
    }


    //查询答卷数量
    public  int queryAnswerCount(){
        return examRecordMapper.queryAnswerCount();
    }

    //查询今日答卷数量
    public   int queryTodayAnswerCount(){
        return examRecordMapper.queryTodayAnswerCount();
    }

    //查询近三天的答卷
    public List<ExamRecord> getRecentAnswerData(){
        return examRecordMapper.getRecentAnswerData();
    };

    public int saveExamRecord(ExamRecord examRecord) {
        return examRecordMapper.insert(examRecord);
    }


    public void updateScoreById(Long record_id, BigDecimal score) {
        examRecordMapper.updateScoreById(record_id,score);
    }
}
